package com.itboye.ihomebank.key;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.ChooseTimePopwindow;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.ChooseTimePopwindow.onFinishedLisenter;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 发送密码(钥匙)
 * 
 */
public class ActivityFaSongPass extends BaseOtherActivity implements
		onFinishedLisenter ,Observer{
	ImageView key_title_finish;
	RelativeLayout key_title_xuanze;
	TextView key_title_xuan;
	TextView fasong_time01;
	TextView fasong_time, end_time;// 时间

	LinearLayout fasong_duanxin, fasong_weixin;// 短信发送，微信发送

	PopupWindow window;
	LinearLayout endTime;
	RelativeLayout deleteTime;
	private int type=0;//选择的类型
	
	LinearLayout shaixuan_main;
	
	private LinearLayout layout;
	private ListView cheng_listView;
	private FrameLayout chengqu_layout;
	private ListView qu_listView;
	List<Map<String, Object>> list;
	List<Map<String, Object>> list01;
	PopupWindow mPopWin;
	
	KeyPresenter keyPresenter;
	
	String lockid;
	
	String uid;
	
	long startT, endT;
	
	long currentTime;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_title_finish:
			finish();
			break;
		case R.id.key_title_xuanze:// 选择框
			typePopup(v);
			break;
		case R.id.fasong_time01:// 选择时间
			ChooseTimePopwindow startTime = new ChooseTimePopwindow(this, this,
					true);
			startTime.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		case R.id.endTime:
			ChooseTimePopwindow endTimePopup = new ChooseTimePopwindow(this,
					this, false);
			endTimePopup.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		case R.id.fasong_duanxin:// 短信发送，
			if(!end_time.getText().toString().equals("")){
				if (endT <= startT) {
					ByAlert.alert("结束时间应该大于开始时间");
					return;
				}
				if (currentTime > endT) {
					ByAlert.alert("结束时间应该大于当前时间");
					return;
				}
			}
			if(fasong_time.getText().toString().equals("")){
				ByAlert.alert("请选择开始日期");
				return;
			}
			showProgressDialog("请稍后...", true);
			keyPresenter.getKeyboardPwd(uid, lockid, type+"", "", startT/1000+"", endT/1000+"");
			
//			Uri smsToUri = Uri.parse("smsto:");
//			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
//			intent.putExtra("sms_body", "测试发送短信");
//			startActivity(intent);
			break;
		case R.id.fasong_weixin:// 微信发送
			ByAlert.alert("开发中，请求期待！");
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_fasong_pass;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		key_title_xuan.setText("单次");
		
		keyPresenter=new KeyPresenter(this);
		
		lockid=this.getIntent().getStringExtra("lockid");
		
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		
		currentTime = System.currentTimeMillis();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		

	}

	@Override
	public void getBeginTime(String year, String month, String day,
			String hour, String minite) {
		startT = TimesUtils.getStringToDateTwo02(year + "-" + month + "-" + day + "  " + hour);
		fasong_time.setText(year + "-" + month + "-" + day + "  " + hour + ":"
				+ minite);
	}

	@Override
	public void getEndTime(String year, String month, String day, String hour,
			String minite) {
		endT = TimesUtils.getStringToDateTwo02(year + "-" + month + "-" + day + "  " + hour + ":"
				+ minite);
		end_time.setText(year + "-" + month + "-" + day + "  " + hour+ ":"
				+ minite);
	}
	
	
	/**
	 * 类型
	 * @param v
	 */
	private void typePopup(View v){
		
		layout = (LinearLayout) LayoutInflater.from(this)
				.inflate(R.layout.popup_chengqu02, null);
		cheng_listView = (ListView) layout.findViewById(R.id.cheng_listView);
		
		chengqu_layout = (FrameLayout) layout.findViewById(R.id.chengqu_layout);
		qu_listView = (ListView) layout.findViewById(R.id.qu_listView);
		
		list=new ArrayList<Map<String, Object>>();
		Map<String, Object> map01=new HashMap<String, Object>();
		map01.put("aaa", "单次");
		Map<String, Object> map02=new HashMap<String, Object>();
		map02.put("aaa", "限时");
		Map<String, Object> map03=new HashMap<String, Object>();
		map03.put("aaa", "永久");
		list.add(map01);
		list.add(map02);
		list.add(map03);
		
		SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.item_choose_area, 
				new String[] {"aaa"}, new int[] {R.id.name});
		
		cheng_listView.setAdapter(adapter);
		
		cheng_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				endTime.setVisibility(View.GONE);
				end_time.setText("");
				if(position==1){
					list01=new ArrayList<Map<String, Object>>();
					Map<String, Object> map01=new HashMap<String, Object>();
					map01.put("bbb", "1小时");
					Map<String, Object> map02=new HashMap<String, Object>();
					map02.put("bbb", "1天");
					Map<String, Object> map03=new HashMap<String, Object>();
					map03.put("bbb", "1周");
					Map<String, Object> map04=new HashMap<String, Object>();
					map04.put("bbb", "1月");
					Map<String, Object> map05=new HashMap<String, Object>();
					map05.put("bbb", "1年");
					list01.add(map01);
					list01.add(map02);
					list01.add(map03);
					list01.add(map04);
					list01.add(map05);
					
					SimpleAdapter adapter01=new SimpleAdapter(MyApplcation.ctx, list01, R.layout.item_choose_area, 
							new String[] {"bbb"}, new int[] {R.id.name});
					chengqu_layout.setVisibility(View.VISIBLE);
					qu_listView.setAdapter(adapter01);
					
				}
				
				if(position==0){
					chengqu_layout.setVisibility(View.VISIBLE);
					mPopWin.dismiss();
					key_title_xuan.setText("单次");
					type=0;
				}
				if(position==2){
					chengqu_layout.setVisibility(View.VISIBLE);
					mPopWin.dismiss();
					key_title_xuan.setText("永久");
					type=7;
				}
				
			}
		});
		qu_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				if(position==0){
					type=2;
				}
				if(position==1){
					type=3;
				}
				if(position==2){
					type=4;
				}
				if(position==3){
					type=5;
				}
				if(position==4){
					type=6;
				}
				key_title_xuan.setText("限时");
				mPopWin.dismiss();
				endTime.setVisibility(View.VISIBLE);
				
			}
		});
		
		mPopWin = new PopupWindow(layout,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopWin.setBackgroundDrawable(new BitmapDrawable());
		mPopWin.showAsDropDown(shaixuan_main, 5, 1);
		mPopWin.update();
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.sendJianPanPass_success) {
				//ByAlert.alert(entity.getData());
				String type01="";
				if(type==0){
					type01="单次钥匙,起始日期为:"+fasong_time.getText().toString();
				}
				if(type==7){
					type01="永久钥匙,起始日期为:"+fasong_time.getText().toString();
				}
				if(type==2){
					type01="限时钥匙，有效期为1小时,起始日期为:"+fasong_time.getText().toString()+",失效日期为"+end_time.getText().toString();
				}
				if(type==3){
					type01="限时钥匙，有效期为天,起始日期为:"+fasong_time.getText().toString()+",失效日期为"+end_time.getText().toString();
				}
				if(type==4){
					type01="限时钥匙，有效期为1周,起始日期为:"+fasong_time.getText().toString()+",失效日期为"+end_time.getText().toString();
				}
				if(type==5){
					type01="限时钥匙，有效期为1个月,起始日期为:"+fasong_time.getText().toString()+",失效日期为"+end_time.getText().toString();
				}
				if(type==6){
					type01="限时钥匙，有效期为1年,起始日期为:"+fasong_time.getText().toString()+",失效日期为"+end_time.getText().toString();
				}
				
				String content="本次发送钥匙为"+type01;
				
				Uri smsToUri = Uri.parse("smsto:");
				Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
				intent.putExtra("sms_body", "本次密码为："+entity.getData().toString()+","+content);
				startActivity(intent);
				
			} else if (entity.getEventType() == KeyPresenter.sendJianPanPass_fail) {
				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		} 
		
	}
}
